package com.yyfax.ph.progress.detail;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Title: ProgressPresenter Description: Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年5月11日 下午7:08:57
 */
public class ProgressDetailPresenter extends Presenter<ProgressDetailModel, ProgressDetailPresenter.View> {

	static final String GET_PROGRESS_DETAIL_FAILURE = "获取进度详情失败!";
	static final String GET_PROGRESS_HISTORY_FAILURE = "获取历史进度失败!";
	static final int GET_PROGRESS_DETAIL = 123;
	static final int GET_PROGRESS_HITSTORY = 124;

	public static class Generator implements PresenterFactory<ProgressDetailPresenter> {
		@Override
		public ProgressDetailPresenter create() {
			return new ProgressDetailPresenter();
		}
	}

	public interface View extends IView {

		void onGetDetailSuccess(ProgressDetailModel.DetailBean detail);

		void onGetDetailFailure(String tips);

		void onGetProgressHistorySuccess(List<ProgressDetailModel.History> list);

		void onGetProgressHistoryFailure(String tips);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new ProgressDetailModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getDetail(String rcmdId) {
		ProgressDetailModel.GetDetailParams detailParams = new ProgressDetailModel.GetDetailParams();
		detailParams.rcmdId = rcmdId;
		execute(detailParams, GET_PROGRESS_DETAIL, false);
	}

	public void getProgressHitstory(String rcmdId) {
		ProgressDetailModel.ProgressHistoryParams params = new ProgressDetailModel.ProgressHistoryParams();
		params.rcmdId = rcmdId;
		execute(params, GET_PROGRESS_HITSTORY, true);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getDetailResult(ProgressDetailModel.DetailResult result) {
		if (isResponseSuccess(result)) {
			mView.onGetDetailSuccess(result.getContent());
		} else {
			if (TextUtils.isEmpty(result.getMsg())) {
				mView.onGetDetailFailure(GET_PROGRESS_DETAIL_FAILURE);
			} else {
				mView.onGetDetailFailure(result.getMsg());
			}
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getProgressHitstoryResult(ProgressDetailModel.HistoryResult result) {
		ProgressDetailModel.ProgressHistory progressHistory = result.getContent();
		if (isResponseSuccess(result) && progressHistory.history.size() > 0) {
			mView.onGetProgressHistorySuccess(progressHistory.history);
		} else {
			mView.onGetProgressHistoryFailure(TextUtils.isEmpty(result.getMsg()) ? GET_PROGRESS_HISTORY_FAILURE : result.getMsg());
		}
	}
}
